/**
 * 
 */
package org.compiere.process;

import java.io.File;
import java.math.BigDecimal;
import java.util.Properties;
import java.util.logging.Level;
import org.compiere.dsr.DSR_DataCollection;
import org.compiere.dsr.DSR_ExcelImport;
import org.compiere.model.I_BPM_Form;
import org.compiere.model.I_BPM_Project;
import org.compiere.model.I_BPM_Subs;
import org.compiere.model.MAttachment;
import org.compiere.model.MAttachmentEntry;
import org.compiere.model.MBPMForm;
import org.compiere.util.Env;
import org.compiere.util.Msg;
import org.joda.time.DateTime;

import extend.org.compiere.utils.Util;

/**
 * @author V.Sokolov
 *
 */
public class BPMSubsFormExport extends SvrProcess {
	
	/** Current context			*/
	private Properties m_ctx;
	int BPM_Project_ID = 0;
	int BPM_Form_ID = 0;
	int BPM_Subs_ID = 0;
	
	/* 
	 */
	@Override
	protected void prepare() {
		
		log.info(" The Sub form export Excel ");
		m_ctx = getCtx();
		
		ProcessInfoParameter[] para = getParameter();
		for (int i = 0; i < para.length; i++)	{
			String name = para[i].getParameterName();
			if (name == null); 
				//
			else if (name.compareTo(I_BPM_Project.COLUMNNAME_BPM_Project_ID) == 0 && para[i].getParameter() != null)
				BPM_Project_ID = new BigDecimal(Integer.parseInt(para[i].getParameter().toString())).intValue();
			else if (name.compareTo(I_BPM_Form.COLUMNNAME_BPM_Form_ID) == 0 && para[i].getParameter() != null)
				BPM_Form_ID = new BigDecimal(Integer.parseInt(para[i].getParameter().toString())).intValue();
			else if (name.compareTo(I_BPM_Subs.COLUMNNAME_BPM_Subs_ID) == 0 && para[i].getParameter() != null)
				BPM_Subs_ID = new BigDecimal(Integer.parseInt(para[i].getParameter().toString())).intValue();
			else
			{
				log.log(Level.INFO, "Unknown Parameter: " + name);
			}
		}
	}

	/* 
	 */
	@Override
	protected String doIt() throws Exception {
		
		String nameProcess = DSR_GenerateReport.class.getName();
		int AD_Process_ID = Util.getAD_Process(nameProcess.substring(nameProcess.lastIndexOf(".")+1, nameProcess.length()));
		//  Prepare Process
		ProcessInfo pi = new ProcessInfo ("The Sub form export Excel", AD_Process_ID);
		//  Prepare Process
		pi.setAD_User_ID (Env.getAD_User_ID(Env.getCtx()));
		pi.setAD_Client_ID(Env.getAD_Client_ID(Env.getCtx()));
		
		String path = Util.localFilePath + "pattern" + new DateTime() + ".xls";
		
		MBPMForm form = new MBPMForm(getCtx(), BPM_Form_ID, get_TrxName());
		MAttachment attachment = form.getAttachment();
		
		MAttachmentEntry entry = null;
		
		if(attachment != null && attachment.getEntries().length > 0)
			entry = attachment.getEntry(0);
		else 
			entry = Util.getAttachment(getProcessInfo(), getCtx(), path);
				
		DSR_DataCollection collection = new DSR_DataCollection(BPM_Form_ID, BPM_Project_ID, BPM_Subs_ID);
		File outFile = DSR_ExcelImport.collectionImport(entry, collection);
		Util.launchFile(outFile);
		
		return Msg.translate(m_ctx, "Success");
	}

}
